﻿/*******************************************************************
 *     JameE
 *******************************************************************
  Copyright 2010 Clifford Champion and JameE contributors.
  
  This file is part of JameE.
  
  JameE is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  JameE is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with JameE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************/
 
 
 
/// <reference path="~/Base/Classer.js" />
/// <reference path="~/Base/IncludeLoader.js" />

$include(
[
	"Engine/SceneObject.js"
	, "Math/Algorithms.js"
],
function () {

	$class(
	"JameE.Assets.Objects.ImageRegion: Engine.SceneObject",
	{
		/////////////////////
		// FIELDS
		/////////////
		_oImage: null,
		_bPreserveSourceAspectRatio: false,


		/////////////////////
		// CONSTRUCTORS
		/////////////
		_ctor: function () {
			var This = this;

			this._bPreserveSourceAspectRatio = $argument("preserveSourceAspectRatio", "boolean") || false;

			var oImage = $argument("image", "Engine.Image");
			if (oImage !== null) {
				this.setImage(oImage);
			}
		},


		/////////////////////
		// METHODS
		/////////////
		draw: function (oContext) {

			// grab image data
			var oImageDesc = this._oImage.getImageDescription();

			// is image ready?
			if (oImageDesc.oImgNode === null) {
				return;
			}

			if (this._oDimensions.fX == 0) {
				this._oDimensions.fX = oImageDesc.oRect.fSpanX;
				this._oDimensions.fY = oImageDesc.oRect.fSpanY;
			}

			//////
			// begin drawing
			oContext.save();
			var fWidth = this._oDimensions.fX;
			var fHeight = this._oDimensions.fY;
			var fHalfWidth = fWidth / 2.0;
			var fHalfHeight = fHeight / 2.0;
			oContext.translate(this._oPosition.fX, this._oPosition.fY);
			oContext.drawImage(
				oImageDesc.oImgNode,
				oImageDesc.oRect.fX,
				oImageDesc.oRect.fY,
				oImageDesc.oRect.fSpanX,
				oImageDesc.oRect.fSpanY,
				-fHalfWidth,
				-fHalfHeight,
				fWidth,
				fHeight
			);

			oContext.restore();
		},

		setImage: function (oImage) {
			$require_type(oImage, "Engine.Image");
			if (oImage === this._oImage) {
				return;
			}

			this._oImage = oImage;
			this._setupImageListener(oImage);
		},

		_setupImageListener: function (oNewImage) {
			var This = this;
			oNewImage.attachOnLoadedListener(
				function (oImage) {
					if (oImage !== This._oImage) {
						// we no longer use this image.
						return;
					}

					var oImageDesc = oImage.getImageDescription();

					if (This._oDimensions === null) { // no dimensions given.
						// use the image's dimensions
						This._setDimensions(oImageDesc.oRect.fSpanX, oImageDesc.oRect.fSpanY);
					}
					else { // dimensions were given
						if (This._bPreserveSourceAspectRatio) { // we need to preserve aspect ratio
							// calculate maximal aspect ratio
							var oDimensions = JameE.Math.Algorithms.maximalRectangle(
								oImageDesc.oRect.fSpanX,
								oImageDesc.oRect.fSpanY,
								This._oDimensions.fX,
								This._oDimensions.fY
							);
							This._setDimensions(oDimensions.fX, oDimensions.fY);
						}
					}
				}
			);
		}
	});

	$include_notify("Assets/Objects/ImageRegion.js");

});
